package com.wkbb.basic.controller.manage;

import com.wkbb.basic.model.Organization;
import com.wkbb.basic.service.GradeService;
import com.wkbb.basic.service.OrganizationService;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.OrganizationDto;
import com.wkbb.common.enums.OrganizationTypeEnum;
import com.wkbb.common.enums.OriginTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Lilin
 */
@RestController
@Api(description = "年级管理(后台管理)")
@RequestMapping("/basic/{apiVersion}/grade")
@Slf4j
public class GradeManageController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private GradeService gradeService;

    @ApiOperation(value = "查询年级列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultDTO list(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "阶段类别(1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科)", required = false) @RequestParam(name = "gradeType", required = false) String gradeType,
            @ApiParam(value = "是否毕业班(null全部 false 否 true是)", required = false) @RequestParam(name = "isTerminate", required = false) Boolean isTerminale) {
        log.info("***********GradeController.list******apiVersion:{},gradeType:{},isTerminale:{}", apiVersion, gradeType, isTerminale);

        try {
            Map<String, Object> params = new HashMap<>(2);
            params.put("gradeType", gradeType);
            params.put("isTerminale", isTerminale == null ? null : (isTerminale ? 1 : 0));

            if(null!=orgId)
            {
                Organization organization = organizationService.getByPrimaryKey(orgId);
                if(null!=organization)
                {
                    if(!StringUtils.isEmpty(organization.getSmartOrgId())&&organization.getOrigin().equals(OriginTypeEnum.SMART_ORG.getValue()))
                    {
                        params.put("orgId", orgId);
                    }
                    else
                    {
                        if(StringUtils.isEmpty(gradeType))
                        {
                            params.put("gradeType", this.getGradeType(organization.getOrgType()));
                        }
                    }
                }
            }

            return new ResultDTO<>(gradeService.getListByMapParams(params));
        } catch (Exception e) {
            log.error("***********OrganizationController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "更新年级缓存")
    @RequestMapping(value = "refreshCache", method = RequestMethod.GET)
    public ResultDTO refreshCache(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion) {
        log.info("***********GradeController.refreshCache******apiVersion:{}", apiVersion);

        try {
            gradeService.refreshCache();

            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********OrganizationController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    //1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科 8培训学校
    private Integer getGradeType(Integer orgType)
    {
        Integer gradeType = 0;
       /* if(OrganizationTypeEnum.KINDERGARTEN.getValue().intValue()==orgType)
        {
            gradeType = 1;
        }
        else if(OrganizationTypeEnum.PRIMARY_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 2;
        }
        else if(OrganizationTypeEnum.SECONDARY_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 3;
        }
        else if(OrganizationTypeEnum.MIDDLE_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 4;
        }
        else if(OrganizationTypeEnum.HIGH_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 5;
        }
        else if(OrganizationTypeEnum.VOCATIONAL_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 6;
        }
        else if(OrganizationTypeEnum.UNIVERSITY.getValue().intValue()==orgType)
        {
            gradeType = 7;
        }
        else if(OrganizationTypeEnum.SIX_CONSISTENCY_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 8;
        }
        else*/ if(OrganizationTypeEnum.NINE_CONSISTENCY_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 9;
        }
        else if(OrganizationTypeEnum.TWELVE_CONSISTENCY_SCHOOL.getValue().intValue()==orgType)
        {
            gradeType = 10;
        }

        return gradeType;
    }
}
